[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern bool GlobalMemoryStatusEx([In, Out] MEMORYSTATUSEX lpBuffer);
The CLR offer us no way to tell us that memory is getting tight. Many think this API provides the best solution. This is mentioned by Jeffrey Richter in his book 'CLR via C#' ISBN: 0-7356-2163-2. It is useful in determining if your system is under excessive memory load by looking at the dwMemoryLoad member of the MEMORYSTATUSEX structure. If this value is > 80 (per Mr. Richter in his discussion of Garbage Collection), it is an indication that you might want to consider converting some strong references into weak references. Remember that a weakreference type will be collected when Generation 0 is full, so it is not a good technique for caching (as many seem to think).
If you encapsulate this in a lower level assembly and wrap this call in a higher level assembly then it will be much easier to change your system when/if Microsoft decides to provide this via a Managed call (or callback). You could also get total memory (and perform a full GC) by calling (as shown in this example):
long tm = GC.GetTotalMemory(true);
or simply:
GC.Collect(GC.MaxGeneration);
private void DisplayMemory()
{
string[] Drives = System.IO.Directory.GetLogicalDrives();
long tm = System.GC.GetTotalMemory(true);
UInt64 UserAvailable=0,Capacity=0,Available=0;
if(Drives!=null)
{
int retval = GetDiskFreeSpaceExA(Drives[0],ref UserAvailable,ref Capacity,ref Available);
this.lblDiskFree.Text = Available.ToString();
this.lblDiskTotal.Text = Capacity.ToString();
}
NativeMethods oMemoryInfo = new NativeMethods();
this.lblMemoryLoadNumber.Text = oMemoryInfo.MemoryLoad.ToString();
this.lblIsMemoryTight.Text = oMemoryInfo.isMemoryTight().ToString();
if (oMemoryInfo.isMemoryTight())
this.lblIsMemoryTight.Text.Font.Bold = true;
else
this.lblIsMemoryTight.Text.Font.Bold = false;
}
[CLSCompliant(false)]
public class NativeMethods {
private MEMORYSTATUSEX msex;
const int MEMORY_TIGHT_CONST = 80;
public bool isMemoryTight()
{
if (_MemoryLoad > 80)
return true;
else
return false;
}
public uint MemoryLoad
{
get { return _MemoryLoad; }
internal set { _MemoryLoad = value; }
}
public NativeMethods() {
msex = new MEMORYSTATUSEX();
if (GlobalMemoryStatusEx(msex)) {
_MemoryLoad = msex.dwMemoryLoad;
//etc.. Repeat for other structure members
}
else
// Use a more appropriate Exception Type. 'Exception' should almost never be thrown
throw new Exception("Unable to initalize the GlobalMemoryStatusEx API");
}
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
private class MEMORYSTATUSEX
{
public uint dwLength;
public uint dwMemoryLoad;
public ulong ullTotalPhys;
public ulong ullAvailPhys;
public ulong ullTotalPageFile;
public ulong ullAvailPageFile;
public ulong ullTotalVirtual;
public ulong ullAvailVirtual;
public ulong ullAvailExtendedVirtual;
public MEMORYSTATUSEX()
{
this.dwLength = (uint) Marshal.SizeOf(typeof(NativeMethods.MEMORYSTATUSEX));
}
}
[DllImport("kernel32.dll")]
private static extern int GetDiskFreeSpaceExA( string lpDirectoryName,
ref System.UInt64 lpFreeBytesAvailable,
ref System.UInt64 lpTotalNumberOfBytes,
ref System.UInt64 lpTotalNumberOfFreeBytes
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern bool GlobalMemoryStatusEx([In, Out] MEMORYSTATUSEX lpBuffer);
}
Do you know one? Please contribute it!